package sqlToGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;

public class HarpalykeGUI1 {

	/**Variable used for resizing the GUI */
	public static final double resizer = 0.78;//1.25;
	/**X dimension of the main window */
	public static final int mainWindowx = 980;
	/**Y dimension of the main window */
	public static final int mainWindowy = 620;
	
	/**Property file holding information about the name of the charts */
	private Properties guiCharts;
	
	/**The main window */
	private Container mainContainer;
	/**The main container */
	private JFrame mainWindow;
	
	/**The sub-frames and containers for holding charts and other
	 * display information */
	private JFrame frame1;
	private Container container1;
	

	private JFrame frame3;
	private Container container3;
	
	private JFrame frame4;
	private Container container4;
	
	/**These frames belong under frame2 */
	private JFrame subframe2_1;
	private Container subcontainer2_1;
	
	private JFrame subframe2_2;
	private Container subcontainer2_2;
	/**--------------------------------*/
	
	/**These frames belong under frame4 */
	private JFrame subframe4_1;
	private Container subcontainer4_1;
	
	private JFrame subframe4_2;
	private Container subcontainer4_2;
	
	private JFrame subframe4_3;
	private Container subcontainer4_3;
	
	private JFrame subframe4_4;
	private Container subcontainer4_4;
	/**--------------------------------*/
	
	private JFrame frame5;
	private Container container5;
	
	private JFrame frame6;
	private Container container6;
	
	/**The tabs displaying other important graphs */
    JTabbedPane tabbedFrames;
    
    private Container tabContainer1;
    private Container tabContainer2;
    private Container tabContainer3;
    private Container tabContainer4;
    private Container tabContainer5;
    private Container tabContainer6;
    private Container tabContainer7;
    /**-------------------------------------------*/
    
	/**Variables used for accessing the created containers
	 * through iterations when adding on the display charts */
	private Container[] indexToContainer;
	private int currentContainer;
	/**Number of charts to load in**/
	private int num_charts = 0;
	
	public HarpalykeGUI1(){

		/**Load the property file for the charts to bed displayed */
		try {
			guiCharts = new Properties();
			guiCharts.load(new FileInputStream
					("properties/guiCharts.properties"));
		} catch (FileNotFoundException e1) {
			System.out.println("HarpalykeGUI1: failed to load property about"+
					"GUI charts");
			System.exit(1);
		} catch (IOException e1) {
			System.out.println("HarpalykeGUI1: failed to load property about"+
					"GUI charts");
			System.exit(1);
		}
		num_charts = Integer.parseInt(guiCharts.getProperty("numOfCharts"));
		
		
		indexToContainer = new Container[num_charts+1];
		currentContainer = 0;
		createGUISkeleton();
	}
	
	private void createGUISkeleton(){
		
		//Create the main JFrame window
		mainWindow = new JFrame("Harpalyke Simulator");
		mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainWindow.setSize((int)(mainWindowx*resizer), 
			(int)(mainWindowy*resizer));
		
		//Create the main container
		mainContainer = mainWindow.getContentPane();
		mainContainer.setLayout(new GridBagLayout());
		mainContainer.setBackground(new Color(0x191970));
		
		//Create the sub-frames for hodling the charts and other 
		//displays of information
		frame1 = new JFrame();
		frame1.setPreferredSize(
				new Dimension((int)(mainWindow.getWidth()*0.20), 
					(int)(mainWindow.getHeight()*0.25)));
		container1 = frame1.getContentPane();
		container1.setBackground(new Color(0xAEB9CA));
		container1.setLayout(new BorderLayout());
		container1.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.20), 
				(int)(mainWindow.getHeight()*0.25)));
		
		/*frame2 = new JFrame();
		frame2.setPreferredSize(
				new Dimension((int)(mainWindow.getWidth()*0.20), 
					(int)(mainWindow.getHeight()*0.2)));
		container2 = frame2.getContentPane();
		container2.setBackground(new Color(0xB6C3D8));
		container2.setLayout(new GridBagLayout());
		container2.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.20), 
				(int)(mainWindow.getHeight()*0.2)));*/
		
		subframe2_1 = new JFrame();
		subframe2_1.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.17), 
				(int)(mainWindow.getHeight()*0.2)));
		subcontainer2_1 = subframe2_1.getContentPane();
		subcontainer2_1.setBackground(new Color(0xBFBCD4));
		subcontainer2_1.setLayout(new BorderLayout());
		subcontainer2_1.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.17), 
				(int)(mainWindow.getHeight()*0.2)));		
		
		subframe2_2 = new JFrame();
		subframe2_2.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.03), 
				(int)(mainWindow.getHeight()*0.2)));
		subcontainer2_2 = subframe2_2.getContentPane();
		subcontainer2_2.setBackground(new Color(0xBFBCD4));
		subcontainer2_2.setLayout(new BorderLayout());
		subcontainer2_2.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.03), 
				(int)(mainWindow.getHeight()*0.2)));
		
		frame3 = new JFrame();
		frame3.setPreferredSize(
				new Dimension((int)(mainWindow.getWidth()*0.20), 
						(int)(mainWindow.getHeight()*0.30)));
		container3 = frame3.getContentPane();
		container3.setBackground(new Color(0xB6B7D8));
		container3.setLayout(new BorderLayout());
		container3.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.20), 
				(int)(mainWindow.getHeight()*0.30)));
		
		frame4 = new JFrame();
		frame4.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.20), 
				(int)(mainWindow.getHeight()*0.25)));
		container4 = frame4.getContentPane();
		container4.setBackground(new Color(0xBFBCD4));
		container4.setLayout(new GridBagLayout());
		container4.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.20), 
				(int)(mainWindow.getHeight()*0.25)));
		
		subframe4_1 = new JFrame();
		subframe4_1.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.05), 
				(int)(mainWindow.getHeight()*0.25)));
		subcontainer4_1 = subframe4_1.getContentPane();
		subcontainer4_1.setBackground(new Color(0xBFBCD4));
		subcontainer4_1.setLayout(new BorderLayout());
		subcontainer4_1.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.05), 
				(int)(mainWindow.getHeight()*0.25)));		
		
		subframe4_2 = new JFrame();
		subframe4_2.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.05), 
				(int)(mainWindow.getHeight()*0.25)));
		subcontainer4_2 = subframe4_2.getContentPane();
		subcontainer4_2.setBackground(new Color(0xBFBCD4));
		subcontainer4_2.setLayout(new BorderLayout());
		subcontainer4_2.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.05), 
				(int)(mainWindow.getHeight()*0.25)));
		
		subframe4_3 = new JFrame();
		subframe4_3.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.05), 
				(int)(mainWindow.getHeight()*0.25)));
		subcontainer4_3 = subframe4_3.getContentPane();
		subcontainer4_3.setBackground(new Color(0xBFBCD4));
		subcontainer4_3.setLayout(new BorderLayout());
		subcontainer4_3.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.05), 
				(int)(mainWindow.getHeight()*0.25)));
		
		subframe4_4 = new JFrame();
		subframe4_4.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.05), 
				(int)(mainWindow.getHeight()*0.25)));
		subcontainer4_4 = subframe4_4.getContentPane();
		subcontainer4_4.setBackground(new Color(0xBFBCD4));
		subcontainer4_4.setLayout(new BorderLayout());
		subcontainer4_4.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.05), 
				(int)(mainWindow.getHeight()*0.25)));
		
		frame5 = new JFrame();
		frame5.setPreferredSize(
				new Dimension((int)(mainWindow.getWidth()*0.72), 
					(int)(mainWindow.getHeight()*1)));
		container5 = frame5.getContentPane();
		container5.setBackground(new Color(0xAEACBE));
		container5.setLayout(new BorderLayout());
		container5.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.72), 
				(int)(mainWindow.getHeight()*1)));
		
		frame6 = new JFrame();
		frame6.setPreferredSize(
				new Dimension((int)(mainWindow.getWidth()*0.08), 
					(int)(mainWindow.getHeight()*1)));
		container6 = frame6.getContentPane();
		container6.setBackground(new Color(0xB5ADC4));
		container6.setLayout(new BorderLayout());
		container6.setPreferredSize(
			new Dimension((int)(mainWindow.getWidth()*0.08), 
				(int)(mainWindow.getHeight()*1)));
	
		
		/**Create the tabs for displaying other relevant graphs */
	    tabbedFrames = new JTabbedPane();
	    tabbedFrames.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

		tabContainer1 = new Container();
		tabContainer1.setLayout(new BorderLayout());
	    
		tabContainer2 = new Container();
		tabContainer2.setLayout(new BorderLayout());

		tabContainer3 = new Container();
		tabContainer3.setLayout(new BorderLayout());
		
		tabContainer4 = new Container();
		tabContainer4.setLayout(new BorderLayout());
			
		tabContainer5 = new Container();
		tabContainer5.setLayout(new BorderLayout());
		
		tabContainer6 = new Container();
		tabContainer6.setLayout(new BorderLayout());

		tabContainer7 = new Container();
		tabContainer7.setLayout(new BorderLayout());
		
		tabbedFrames.addTab("<html>  <style type='text/css'> "+
				"body { "+
				"color: 502B6F; "+
				"width: " +(int)(mainWindow.getWidth()*0.72*0.12) +"px; "+
				"font-size: " +(int)(mainWindow.getWidth()*0.72*0.025) +"px; "+
				"height: " +(int)(mainWindow.getWidth()*0.72*0.02) +"px; "+
	            "} </style> " +
	            "<body " +
	            "marginwidth=" + 3 + " " +
				"marginheight=5>" + guiCharts.getProperty("chart9TabName") + 
				"</body></html>", 
				tabContainer1);
		
		tabbedFrames.addTab("<html>  <style type='text/css'> "+
				"body { "+
				"color: 502B6F; "+
				"width: " +(int)(mainWindow.getWidth()*0.72*0.12) +"px; "+
				"font-size: " +(int)(mainWindow.getWidth()*0.72*0.025) +"px; "+
				"height: " +(int)(mainWindow.getWidth()*0.72*0.02) +"px; "+
	            "} </style> " +
	            "<body " +
	            "marginwidth=" + 3 + " " +
				"marginheight=5>" + guiCharts.getProperty("chart10TabName") + 
				"</body></html>", 
				tabContainer2);
		
		tabbedFrames.addTab("<html>  <style type='text/css'> "+
				"body { "+
				"color: 502B6F; "+
				"width: " +(int)(mainWindow.getWidth()*0.72*0.12) +"px; "+
				"font-size: " +(int)(mainWindow.getWidth()*0.72*0.025) +"px; "+
				"height: " +(int)(mainWindow.getWidth()*0.72*0.02) +"px; "+
	            "} </style> " +
	            "<body " +
	            "marginwidth=" + 3 + " " +
				"marginheight=5>" + guiCharts.getProperty("chart11TabName") + 
				"</body></html>", 
				tabContainer3);
		
		tabbedFrames.addTab("<html>  <style type='text/css'> "+
			"body { "+
			"color: 502B6F; "+
			"width: " +(int)(mainWindow.getWidth()*0.72*0.12) +"px; "+
			"font-size: " +(int)(mainWindow.getWidth()*0.72*0.025) +"px; "+
			"height: " +(int)(mainWindow.getWidth()*0.72*0.02) +"px; "+
            "} </style> " +
            "<body " +
            "marginwidth=" + 3 + " " +
			"marginheight=5>" + guiCharts.getProperty("chart12TabName") + 
			"</body></html>", 
			tabContainer4);
	
		tabbedFrames.addTab("<html>  <style type='text/css'> "+
				"body { "+
				"color: 502B6F; "+
				"width: " +(int)(mainWindow.getWidth()*0.72*0.12) +"px; "+
				"font-size: " +(int)(mainWindow.getWidth()*0.72*0.025) +"px; "+
				"height: " +(int)(mainWindow.getWidth()*0.72*0.02) +"px; "+
	            "} </style> " +
	            "<body " +
	            "marginwidth=3 " +
				"marginheight=5>" + guiCharts.getProperty("chart13TabName") + 
				"</body></html>", 
				tabContainer5);
		
		tabbedFrames.addTab("<html>  <style type='text/css'> "+
				"body { "+
				"color: 502B6F; "+
				"width: " +(int)(mainWindow.getWidth()*0.72*0.12) +"px; "+
				"font-size: " +(int)(mainWindow.getWidth()*0.72*0.025) +"px; "+
				"height: " +(int)(mainWindow.getWidth()*0.72*0.02) +"px; "+
	            "} </style> " +
	            "<body " +
	            "marginwidth=3 " +
				"marginheight=5>" + guiCharts.getProperty("chart14TabName") + 
				"</body></html>", 
				tabContainer6);
		
		tabbedFrames.addTab("<html>  <style type='text/css'> "+
				"body { "+
				"color: 502B6F; "+
				"width: " +(int)(mainWindow.getWidth()*0.72*0.12) +"px; "+
				"font-size: " +(int)(mainWindow.getWidth()*0.72*0.025) +"px; "+
				"height: " +(int)(mainWindow.getWidth()*0.72*0.02) +"px; "+
	            "} </style> " +
	            "<body " +
	            "marginwidth=3 " +
				"marginheight=5>" + guiCharts.getProperty("chart15TabName") + 
				"</body></html>", 
				tabContainer7);	
		
	    tabbedFrames.setBackgroundAt(0, new Color(0xCDCDA4));
	    tabbedFrames.setBackgroundAt(1, new Color(0xCDCDA4));
	    tabbedFrames.setBackgroundAt(2, new Color(0xCDCDA4));
	    tabbedFrames.setBackgroundAt(3, new Color(0xCDCDA4));
	    tabbedFrames.setBackgroundAt(4, new Color(0xCDCDA4));
	    tabbedFrames.setBackgroundAt(5, new Color(0xCDCDA4));
	    tabbedFrames.setBackgroundAt(6, new Color(0xCDCDA4));
	    
		frame5.add(tabbedFrames);
		
		/**Add the frames to a hashtable so that they can be
		 * referenced by index later on */
		indexToContainer[0] = container1;

		indexToContainer[1] = subcontainer2_1;
		indexToContainer[2] = subcontainer2_2;
		
		indexToContainer[3] = container3;

		indexToContainer[4] = subcontainer4_1;
		indexToContainer[5] = subcontainer4_2;
		indexToContainer[6] = subcontainer4_3;
		indexToContainer[7] = subcontainer4_4;

		indexToContainer[8] = tabContainer1;
		indexToContainer[9] = tabContainer2;
		indexToContainer[10] = tabContainer3;
		indexToContainer[11] = tabContainer4;
		indexToContainer[12] = tabContainer5;
		indexToContainer[13] = tabContainer6;
		indexToContainer[14] = tabContainer7;
		
		indexToContainer[15] = container6;
	}
	
	/**Resise the GUI to an appropriate looking size */
	public void setNiceFrameSize(){
		
		Insets insets = new Insets((int)(2*resizer), (int)(2.5*resizer),
				(int)(2*resizer), (int)(2.5*resizer)); 
		
		mainContainer.add(container1, new GridBagConstraints(
				0, 
				0, 
						
				(int)(mainWindow.getWidth()*0.20), 
				(int)(mainWindow.getHeight()*0.25), 
						
				1.0, 1.0, GridBagConstraints.CENTER, 
				GridBagConstraints.BOTH, insets, 0, 0));
		
		mainContainer.add(subcontainer2_1, new GridBagConstraints(
				0, 
				(int)(mainWindow.getHeight()*0.25), 
				
				(int)(mainWindow.getWidth()*0.17), 
				(int)(mainWindow.getHeight()*0.20), 
				
				1.0, 1.0, GridBagConstraints.CENTER, 
				GridBagConstraints.BOTH, insets, 0, 0));
		
		mainContainer.add(subcontainer2_2, new GridBagConstraints(
				(int)(mainWindow.getWidth()*0.17), 
				(int)(mainWindow.getHeight()*0.25), 
				
				(int)(mainWindow.getWidth()*0.03), 
				(int)(mainWindow.getHeight()*0.20), 
				
				1.0, 1.0, GridBagConstraints.CENTER, 
				GridBagConstraints.BOTH, insets, 0, 0));
		
		/*mainContainer.add(container2, new GridBagConstraints(
				0, 
				(int)(mainWindow.getHeight()*0.25), 
				
				(int)(mainWindow.getWidth()*0.20), 
				(int)(mainWindow.getHeight()*0.2), 
				
				1.0, 1.0, GridBagConstraints.CENTER, 
				GridBagConstraints.BOTH, insets, 0, 0));*/
		
		mainContainer.add(container3, new GridBagConstraints(
				0, 
				(int)(mainWindow.getHeight()*0.45), 
				
				(int)(mainWindow.getWidth()*0.20), 
				(int)(mainWindow.getHeight()*0.30), 
				
				1.0, 1.0, GridBagConstraints.CENTER, 
				GridBagConstraints.BOTH, insets, 0, 0));
		
		container4.add(subcontainer4_1, new GridBagConstraints(
				0, 
				(int)(mainWindow.getHeight()*0.55), 
				
				(int)(mainWindow.getWidth()*0.05), 
				(int)(mainWindow.getHeight()*0.20), 
				
				1.0, 1.0, GridBagConstraints.CENTER, 
				GridBagConstraints.BOTH, insets, 0, 0));
		
		container4.add(subcontainer4_2, new GridBagConstraints(
				(int)(mainWindow.getWidth()*0.05), 
				(int)(mainWindow.getHeight()*0.55), 
				
				(int)(mainWindow.getWidth()*0.05), 
				(int)(mainWindow.getHeight()*0.20), 
				
				1.0, 1.0, GridBagConstraints.CENTER, 
				GridBagConstraints.BOTH, insets, 0, 0));
		
		container4.add(subcontainer4_3, new GridBagConstraints(
				(int)(mainWindow.getWidth()*0.1), 
				(int)(mainWindow.getHeight()*0.55), 
				
				(int)(mainWindow.getWidth()*0.05), 
				(int)(mainWindow.getHeight()*0.20), 
				
				1.0, 1.0, GridBagConstraints.CENTER, 
				GridBagConstraints.BOTH, insets, 0, 0));
		
		container4.add(subcontainer4_4, new GridBagConstraints(
				(int)(mainWindow.getWidth()*0.15), 
				(int)(mainWindow.getHeight()*0.55), 
				
				(int)(mainWindow.getWidth()*0.05), 
				(int)(mainWindow.getHeight()*0.20), 
				
				1.0, 1.0, GridBagConstraints.CENTER, 
				GridBagConstraints.BOTH, insets, 0, 0));
		
		mainContainer.add(container4, new GridBagConstraints(
				0, 
				(int)(mainWindow.getHeight()*0.75), 
				(int)(mainWindow.getWidth()*0.20), 
				(int)(mainWindow.getHeight()*0.25), 
				
				1.0, 1.0, GridBagConstraints.CENTER, 
				GridBagConstraints.BOTH, insets, 0, 0));
		
		mainContainer.add(container5, new GridBagConstraints(
				(int)(mainWindow.getWidth()*0.20), 
				(int)(mainWindow.getHeight()*0.0), 
				
				(int)(mainWindow.getWidth()*0.72), 
				(int)(mainWindow.getHeight()*1.0), 
				
				1.0, 1.0, GridBagConstraints.CENTER, 
				GridBagConstraints.BOTH, insets, 0, 0));

		mainContainer.add(container6, new GridBagConstraints(
				(int)(mainWindow.getWidth()*0.92), 
				(int)(mainWindow.getHeight()*0.0), 
				
				(int)(mainWindow.getWidth()*0.08), 
				(int)(mainWindow.getHeight()*1.0), 
				
				1.0, 1.0, GridBagConstraints.CENTER, 
				GridBagConstraints.BOTH, insets, 0, 0));

		mainWindow.setVisible(true);
		mainWindow.pack();
		
	}
	
	/**Add a display chart to the main skeleton of the GUI */
	public void addDisplayChart(Container component) {
		if(component == null) {
			/**DO NOTHING IF NO CHART ASKED FOR */
		} else if (component != null) {
			indexToContainer[currentContainer].add(component);
		} else{}
		
		currentContainer++;
		mainWindow.setVisible(true);
	}

}
